﻿using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using UnityEngine;
using WssEvents;
using WssUtils;

namespace JQMergeFramework
{
    public class CityBaseControl : MonoBehaviour
    {
        public static CityBaseControl I;
        
        [Header("放置房子父物体")] public Transform cityPlaceParent;

        [Header("棋盘Collider容器")] public Transform groundParent;

        [Header("大房子模型父物体")] public Transform bigCityParent;

        [Header("棋盘模型")] public GameObject chessboard;
        
        [Header("房子放置平台模型")] public GameObject cityPlatform;

        //格子大小
        private Vector3 mGroundTemplateSize;

        private GameObject groundTemplatePrefab;
        //棋盘格子间隔
        private float groundSpace = 0.1F;
        

        //已创建的房子列表
        private List<MainCityUnit> mainCityNodeList = new List<MainCityUnit>();
        
        public int cloneCityCount
        {
            get { return mainCityNodeList.Count; }
        }

        public Vector3 GroundTemplateScale
        {
            get { return mGroundTemplateSize; }
        }
        
        //房子的平台高度
        public float cityPlatformHeight;
        
        //房子节点高度
        private float[] cityNodeHeightAry;

        //棋盘模型大小
        public Vector3 chessboardSize;

        private void Awake()
        {
            I = this;
            MergeManager mergeManager = MergeManager.Instance;
            
            cityPlatformHeight = cityPlatform.transform.GetChild(0).GetComponent<MeshRenderer>().bounds.size.y;
            Mesh[] cityNodeAry = mergeManager.scriptablePrefab.mainCityMeshList;
            
            cityNodeHeightAry = new float[cityNodeAry.Length];
            for (int i = 0; i < cityNodeAry.Length; i++)
            {
                cityNodeHeightAry[i] = cityNodeAry[i].bounds.size.y;
            }

            chessboardSize = chessboard.transform.GetChild(0).GetComponent<MeshRenderer>().bounds.size;

            groundTemplatePrefab = mergeManager.scriptablePrefab.groundTemplate;


            MeshCollider groundTemplateMeRender = groundTemplatePrefab.GetComponent<MeshCollider>();
            mGroundTemplateSize = groundTemplateMeRender.sharedMesh.bounds.size;
        }

        private void Start()
        {

            float dis = MainCityControl.I.GetCityNodeHeight();
            
            this.groundParent.localPosition =
                new Vector3(0, dis - GroundTemplateScale.y, 0);
            
            chessboard.transform.localPosition = new Vector3(0,
                MainCityControl.I.GetCityNodeHeight(false) - GroundTemplateScale.y, 0);

            this.cityPlaceParent.localPosition = new Vector3(0, dis, 0);
            ChessboardMove();
        }


        private void OnDisable()
        {
            GlobalEvent.Removelistener(GameEvents.OnGameStart, InitGround);
        }

        private void OnEnable()
        {
            GlobalEvent.Addlistener(GameEvents.OnGameStart, InitGround);
        }

        #region 初始化棋盘box

        void InitGround()
        {
            //正方形棋盘生成
            int row = MergeManager.Instance.row;
            int col = MergeManager.Instance.col;
            GroundBase[,] groundArray = new GroundBase[row, col];
            
            //六变形棋盘生成
            //中心点偏移数量
            int centralPoint = 2;
            //六边形大小
            float adjoinX = GroundTemplateScale.x;
            for (int y = 0; y < row; y++)
            {
                for (int x = 0; x < col; x++)
                {
                    //中心点 2/2
                    float offX = y % 2 != 0 ? -adjoinX/2 : 0;
                    GameObject cloneGo = Instantiate(groundTemplatePrefab, this.groundParent);
                    cloneGo.transform.localPosition = new Vector3((x - centralPoint) * adjoinX + offX, 0, (y-centralPoint) * (GroundTemplateScale.z*0.75F));
                    groundArray[y, x] = cloneGo.GetComponent<GroundBase>();
                    groundArray[y, x].InitData(y, x);
                    cloneGo.name = string.Format("x:{0}_y:{1}", y,x);
                }
            }

            //禁用一些格子。
            for (int i = 0; i < groundArray.GetLength(0); i++)
            {
                for (int j = 0; j < groundArray.GetLength(1); j++)
                {
                    if (i != 2)
                    {
                        if (j == 0)
                        {
                            groundArray[i, j].SetDisabled(true);
                        }
                        else if (j+1 == col && (i == 0 || i == 4))
                        {
                            groundArray[i, j].SetDisabled(true);
                        }
                    }
                }
            }
            
            
            MergeManager.Instance.groundArray = groundArray;
            //开始游戏
            MergeManager.Instance.StartPlaceThree();
        }

        #endregion

        
  
        /// <summary>
        /// 棋盘位置设置
        /// </summary>
        /// <param name="isAnim">是否播放Twen动画</param>
        public void ChessboardMove(bool isAnim = false)
        {
            if (isAnim)
            {
                chessboard.transform.DOMoveY(MainCityControl.I.GetCityNodeHeight(false) - GroundTemplateScale.y,
                    0.5F);
            }
            else
            {
                chessboard.transform.position = new Vector3(0, MainCityControl.I.GetCityNodeHeight(false) - GroundTemplateScale.y, 0);
            }

            float dis = MainCityControl.I.GetCityNodeHeight();
            this.groundParent.position =
                new Vector3(0, dis - GroundTemplateScale.y, 0);
            
            this.cityPlaceParent.localPosition = new Vector3(0, dis, 0);
            
            SetShowChessboard(true);
        }
 
        
        //棋盘展示隐藏
        public void SetShowChessboard(bool isShow)
        {
            //棋盘
            chessboard.SetActive(isShow);
            cityPlaceParent.gameObject.SetActive(isShow);
        }
        

    }
}